package com.apes.fn.scm.retailOrder.repository;

import com.apes.fn.scm.retailOrder.model.RetailOrderDiscountItem;
import com.apes.framework.jpa.repository.CustomRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author HuJianlin
 * @create 2018-10-31 16:07
 */
public interface RetailOrderDiscountItemRepository extends CustomRepository<RetailOrderDiscountItem, String> {

    List<RetailOrderDiscountItem> findAllByRetailOrderId(String retailOrderId);

    @Query(value = "select t from RetailOrderDiscountItem t  " +
            "            where t.retailOrderId in (:retailOrderIds) and t.platformSubsidy <>0")
    List<RetailOrderDiscountItem> findAllByRetailOrderIdsAndPlatformSubsidy(List retailOrderIds);


    @Query("select T.company.id AS companyId,T.dept.id AS deptId,T.vipAlliance.id AS vipAllianceId,T.type AS type " +
            "from RetailOrderDiscountItem T where T.retailDiscount.id = :id " +
            "GROUP BY T.company.id,T.dept.id,T.vipAlliance.id,T.type")
    List<Map> findByDiscountId(@Param("id") String id);

    @Query("SELECT T FROM RetailOrderDiscountItem T WHERE T.retailDiscount.id = :discountId " +
            "AND T.company.id = :companyId AND T.dept.id = :deptId " +
            "AND T.vipAlliance.id = :vipAllianceId AND T.type = :type")
    List<RetailOrderDiscountItem> findByDiscountIdCompanyIdDeptIdVipAllianceId(@Param("discountId") String id, @Param("companyId") String companyId,
                                                                               @Param("deptId") String deptId, @Param("vipAllianceId") String vipAllianceId,
                                                                               @Param("type") String type);

    @Query("select new Map(t.retailOrderId as retailOrderId) from RetailOrderDiscountItem t " +
            " where t.type = 0 and t.createDate >= :startDate and t.createDate <= :endDate")
    Page<Map<String, String>> findAllForUpdateDiscount(@Param("startDate") Date startDate, @Param("endDate") Date endDate, Pageable pageable);

}
